﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.DataAccess.DO
{
    public partial class ReceiptDetailDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static ReceiptDetailDO GetModelByPK(Session sess, int iIntRecLineNum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "RCP_RReceiptDetailByPK";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_RECEIPT_LINE_NUM", iIntRecLineNum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_RECEIPT_LINE_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "RCP_IReceiptDetail";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "RCP_DReceiptDetailByPK";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(this));
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public string Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "RCP_UReceiptDetail";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return null;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE RECEIPT_DETAIL SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_RECEIPT_LINE_NUM = @INTERNAL_RECEIPT_LINE_NUM";
                paras[i] = new SqlParameter("@INTERNAL_RECEIPT_LINE_NUM", this.INTERNAL_RECEIPT_LINE_NUM);

                helper.Update(CommandType.Text, stSQL, paras);

                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象RECEIPDETAIL的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<ReceiptDetailDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "RCP_RReceiptDetail";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性RECEIPDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <remarks></remarks>
        public static List<ReceiptDetailDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }


        /// <summary>
        /// 从DataView装载持久性RECEIPDETAIL对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<ReceiptDetailDO> LoadListFromDataView(DataView dv)
        {
            List<ReceiptDetailDO> list = new List<ReceiptDetailDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }


        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static ReceiptDetailDO GetModel(DataRow row)
        {
            ReceiptDetailDO recDtlDO = new ReceiptDetailDO();
            if (!row["INTERNAL_RECEIPT_LINE_NUM"].Equals(DBNull.Value)) recDtlDO.INTERNAL_RECEIPT_LINE_NUM = Convert.ToInt32(row["INTERNAL_RECEIPT_LINE_NUM"]);
            if (!row["INTERNAL_RECEIPT_NUM"].Equals(DBNull.Value)) recDtlDO.INTERNAL_RECEIPT_NUM = Convert.ToInt32(row["INTERNAL_RECEIPT_NUM"]);
            if (!row["RECEIPT_ID"].Equals(DBNull.Value)) recDtlDO.RECEIPT_ID = Convert.ToString(row["RECEIPT_ID"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) recDtlDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) recDtlDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["ITEM"].Equals(DBNull.Value)) recDtlDO.ITEM = Convert.ToString(row["ITEM"]);
            if (!row["ITEM_DESC"].Equals(DBNull.Value)) recDtlDO.ITEM_DESC = Convert.ToString(row["ITEM_DESC"]);
            if (!row["ITEM_CLASS"].Equals(DBNull.Value)) recDtlDO.ITEM_CLASS = Convert.ToString(row["ITEM_CLASS"]);
            if (!row["ATTRIBUTE_NUM"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE_NUM = Convert.ToInt32(row["ATTRIBUTE_NUM"]);
            if (!row["ATTRIBUTE1"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE1 = Convert.ToString(row["ATTRIBUTE1"]);
            if (!row["ATTRIBUTE2"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE2 = Convert.ToString(row["ATTRIBUTE2"]);
            if (!row["ATTRIBUTE3"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE3 = Convert.ToString(row["ATTRIBUTE3"]);
            if (!row["ATTRIBUTE4"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE4 = Convert.ToString(row["ATTRIBUTE4"]);
            if (!row["ATTRIBUTE5"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE5 = Convert.ToString(row["ATTRIBUTE5"]);
            if (!row["ATTRIBUTE6"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE6 = Convert.ToString(row["ATTRIBUTE6"]);
            if (!row["ATTRIBUTE7"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE7 = Convert.ToString(row["ATTRIBUTE7"]);
            if (!row["ATTRIBUTE8"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE8 = Convert.ToString(row["ATTRIBUTE8"]);
            if (!row["TOTAL_QTY"].Equals(DBNull.Value)) recDtlDO.TOTAL_QTY = Convert.ToInt32(row["TOTAL_QTY"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) recDtlDO.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["OPEN_QTY"].Equals(DBNull.Value)) recDtlDO.OPEN_QTY = Convert.ToInt32(row["OPEN_QTY"]);
            if (!row["ERP_ORDER_ID"].Equals(DBNull.Value)) recDtlDO.ERP_ORDER_ID = Convert.ToString(row["ERP_ORDER_ID"]);
            if (!row["ERP_ORDER_LINE_NUM"].Equals(DBNull.Value)) recDtlDO.ERP_ORDER_LINE_NUM = Convert.ToInt32(row["ERP_ORDER_LINE_NUM"]);
            if (!row["LOCATING_RULE"].Equals(DBNull.Value)) recDtlDO.LOCATING_RULE = Convert.ToString(row["LOCATING_RULE"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) recDtlDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) recDtlDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["ATTRIBUTE_TRACK"].Equals(DBNull.Value)) recDtlDO.ATTRIBUTE_TRACK = Convert.ToString(row["ATTRIBUTE_TRACK"]);
            if (!row["INVENTORY_STS"].Equals(DBNull.Value)) recDtlDO.INVENTORY_STS = Convert.ToString(row["INVENTORY_STS"]);
            return recDtlDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [RECEIPT_DETAIL] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(ReceiptDetailDO recDtlDO)
        {
            SqlParameter[] paras = new SqlParameter[27];
            paras[0] = new SqlParameter("@INTERNAL_RECEIPT_LINE_NUM", recDtlDO.INTERNAL_RECEIPT_LINE_NUM);
            paras[1] = new SqlParameter("@INTERNAL_RECEIPT_NUM", recDtlDO.INTERNAL_RECEIPT_NUM == 0 ? DBNull.Value : (object)recDtlDO.INTERNAL_RECEIPT_NUM);
            paras[2] = new SqlParameter("@RECEIPT_ID", recDtlDO.RECEIPT_ID == String.Empty ? DBNull.Value : (object)recDtlDO.RECEIPT_ID);
            paras[3] = new SqlParameter("@WAREHOUSE", recDtlDO.WAREHOUSE == String.Empty ? DBNull.Value : (object)recDtlDO.WAREHOUSE);
            paras[4] = new SqlParameter("@COMPANY", recDtlDO.COMPANY == String.Empty ? DBNull.Value : (object)recDtlDO.COMPANY);
            paras[5] = new SqlParameter("@ITEM", recDtlDO.ITEM == String.Empty ? DBNull.Value : (object)recDtlDO.ITEM);
            paras[6] = new SqlParameter("@ITEM_DESC", recDtlDO.ITEM_DESC == String.Empty ? DBNull.Value : (object)recDtlDO.ITEM_DESC);
            paras[7] = new SqlParameter("@ITEM_CLASS", recDtlDO.ITEM_CLASS == String.Empty ? DBNull.Value : (object)recDtlDO.ITEM_CLASS);
            paras[8] = new SqlParameter("@ATTRIBUTE_NUM", recDtlDO.ATTRIBUTE_NUM == 0 ? DBNull.Value : (object)recDtlDO.ATTRIBUTE_NUM);
            paras[9] = new SqlParameter("@ATTRIBUTE1", recDtlDO.ATTRIBUTE1 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE1);
            paras[10] = new SqlParameter("@ATTRIBUTE2", recDtlDO.ATTRIBUTE2 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE2);
            paras[11] = new SqlParameter("@ATTRIBUTE3", recDtlDO.ATTRIBUTE3 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE3);
            paras[12] = new SqlParameter("@ATTRIBUTE4", recDtlDO.ATTRIBUTE4 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE4);
            paras[13] = new SqlParameter("@ATTRIBUTE5", recDtlDO.ATTRIBUTE5 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE5);
            paras[14] = new SqlParameter("@ATTRIBUTE6", recDtlDO.ATTRIBUTE6 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE6);
            paras[15] = new SqlParameter("@ATTRIBUTE7", recDtlDO.ATTRIBUTE7 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE7);
            paras[16] = new SqlParameter("@ATTRIBUTE8", recDtlDO.ATTRIBUTE8 == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE8);
            paras[17] = new SqlParameter("@TOTAL_QTY", recDtlDO.TOTAL_QTY);
            paras[18] = new SqlParameter("@QUANTITY_UM", recDtlDO.QUANTITY_UM == String.Empty ? DBNull.Value : (object)recDtlDO.QUANTITY_UM);
            paras[19] = new SqlParameter("@OPEN_QTY", recDtlDO.OPEN_QTY);
            paras[20] = new SqlParameter("@ERP_ORDER_ID", recDtlDO.ERP_ORDER_ID == String.Empty ? DBNull.Value : (object)recDtlDO.ERP_ORDER_ID);
            paras[21] = new SqlParameter("@ERP_ORDER_LINE_NUM", recDtlDO.ERP_ORDER_LINE_NUM);
            paras[22] = new SqlParameter("@LOCATING_RULE", recDtlDO.LOCATING_RULE == String.Empty ? DBNull.Value : (object)recDtlDO.LOCATING_RULE);
            paras[23] = new SqlParameter("@USER_STAMP", recDtlDO.USER_STAMP == String.Empty ? DBNull.Value : (object)recDtlDO.USER_STAMP);
            paras[24] = new SqlParameter("@DATE_TIME_STAMP", System.DateTime.Now);
            paras[25] = new SqlParameter("@ATTRIBUTE_TRACK", recDtlDO.ATTRIBUTE_TRACK == String.Empty ? DBNull.Value : (object)recDtlDO.ATTRIBUTE_TRACK);
            paras[26] = new SqlParameter("@INVENTORY_STS", recDtlDO.INVENTORY_STS == String.Empty ? DBNull.Value : (object)recDtlDO.INVENTORY_STS);


            paras[0].DbType = DbType.Decimal;
            paras[1].DbType = DbType.Decimal;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.String;
            paras[6].DbType = DbType.String;
            paras[7].DbType = DbType.String;
            paras[8].DbType = DbType.String;
            paras[9].DbType = DbType.String;
            paras[10].DbType = DbType.String;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.String;
            paras[15].DbType = DbType.String;
            paras[16].DbType = DbType.String;
            paras[17].DbType = DbType.Decimal;
            paras[18].DbType = DbType.String;
            paras[19].DbType = DbType.Decimal;
            paras[20].DbType = DbType.String;
            paras[21].DbType = DbType.Decimal;
            paras[22].DbType = DbType.String;
            paras[23].DbType = DbType.String;
            paras[24].DbType = DbType.DateTime;
            paras[25].DbType = DbType.String;
            paras[26].DbType = DbType.String;


            return paras;
        }

        private static IDataParameter[] UpdateWherePara(ReceiptDetailDO recDtlDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_RECEIPT_LINE_NUM", recDtlDO.INTERNAL_RECEIPT_LINE_NUM);
            return paras;
        }

        #endregion

        public static List<ReceiptDetailDO> GetByCrit(Session sess, string stWhere)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            StringBuilder sbSQL = null;
            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(ReceiptDetailDO.SelectSequel).Append(stWhere);
                dtModel = helper.GetTable(CommandType.Text, sbSQL.ToString());
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }


        public static List<ReceiptDetailDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}

